package com.onec.service.mybatis.uitil;

import com.onec.service.stream.util.Person;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.jdbc.SQL;
import org.apache.ibatis.jdbc.ScriptRunner;
import org.apache.ibatis.jdbc.SqlRunner;
import org.apache.ibatis.reflection.DefaultReflectorFactory;
import org.apache.ibatis.reflection.MetaClass;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.reflection.SystemMetaObject;
import org.apache.ibatis.reflection.factory.DefaultObjectFactory;
import org.apache.ibatis.reflection.factory.ObjectFactory;
import org.apache.ibatis.reflection.invoker.Invoker;

public class SqlUtil {


  public void createSql() {

    String selectSql = new SQL() {{
      SELECT("P.ID,P.TEST");
      SELECT("P.ID,P.TEST");
      FROM("p_USER");
      FROM("A_USER");
      INNER_JOIN("");
      WHERE("P.id=A.id");
      GROUP_BY("");
      HAVING("");
      OR();
      ORDER_BY("");
    }}.toString();

    String insertSql = new SQL().INSERT_INTO("t_user_inf")
        .VALUES("id,testss", "#{id,#{testss}}")
        .VALUES("last_name", "#{lastName}").toString();
  }


  public void scriptRunner() {

    try {
      Class.forName("");
      Connection connection = DriverManager.getConnection("", "", "");

      ScriptRunner scriptRunner = new ScriptRunner(connection);
      scriptRunner.runScript(Resources.getResourceAsReader("222222.sql"));
    } catch (SQLException e) {
      e.printStackTrace();
    } catch (ClassNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }


  public void sqlRunner() throws ClassNotFoundException, SQLException {
    Class.forName("");
    Connection connection = DriverManager.getConnection("", "", "");

    SqlRunner sqlRunner = new SqlRunner(connection);
    String sql = new SQL().INNER_JOIN("").VALUES("", "#{}").toString();
    sqlRunner.insert(sql, "");

    sqlRunner.selectAll("", "");
  }


  public void metaObjectTest() {

    Person person = new Person();
    MetaObject metaObject = SystemMetaObject.forObject(person);
    metaObject.getValue("usus[0].getName");

    // 设置属性
    metaObject.setValue("person[0].name", "测试名称");
    // 判断对象是否有属性 name
    metaObject.hasGetter("name");
  }

  public void metaClassTest() {

    MetaClass metaClass = MetaClass.forClass(Person.class, new DefaultReflectorFactory());
    String[] fileds = metaClass.getGetterNames();
    Invoker invoker = metaClass.getGetInvoker("orderNo");

  }


  public void objectFactory() {
    ObjectFactory objectFactory = new DefaultObjectFactory();
    List<Integer> list = objectFactory.create(List.class);
    Map<String, String> stringStringMap = objectFactory.create(Map.class);


  }

}

// 主配置文件中加入标签：
//public class customerObjectFactory extends DefaultObjectFactory {
//
//  @Override
//  public <T> T create(Class<T> type) {
//    if (type.equals(Person.class)) {
//      Person p = (Person) super.create(type);
//
//    }
//
//    return super.create(type);
//  }
//}

